<?php echo $html->script("lib/jquery.datepicker.min.js");?>
<?php echo $html->script("lib/swfupload/swfupload.js");?>
<?php echo $html->script("lib/swfupload/swfupload.queue.js");?>
<?php echo $html->script("lib/swfupload/swfupload.setting.js");?>
<?php echo $html->script("lib/swfupload/fileprogress.js");?>
<?php echo $html->script("lib/swfupload/handlers.js");?>
<?php echo $html->css("screen/jquery.datepicker.css");?>
<?php echo $html->css("screen/stocklist.css");?>
<?php echo $html->css("screen/swfupload.css");?>
<?php echo $html->css("screen/press_admin.css");?>
<script  type="text/javascript">
	var MAX_FILE=4;
	var PATHUPLOAD="<?php echo $this->webroot;?>files/uploads/<?php echo $pathupload;?>/";
	var FOLDERUPLOAD="<?php echo $pathupload;?>/";
	var FILEUPLOAD="<?php echo $fileupload;?>";
	var swfu;
	var uploads = new Array();
	function cancelUpload(id,fname){
		$("#"+id).hide();
		deleteUploadfile(fname);	
	}
	function fileQueued(file) {
		try {
			var progress = new FileProgress(file, this.customSettings.progressTarget);		
				progress.setStatus("Pending...");
				progress.toggleCancel(true, this);		
		} catch (ex) {
			this.debug(ex);
		}

	}
	function uploadSuccess1(file, serverData) {
		try {		
			fileuploaded = FILEUPLOAD+"_"+file.index+file.type;		
			//$("#thumbnail").html('<a class="upload_close" href="#" target="_self" onclick="cancelUpload();return false;">&nbsp;&nbsp;</a><img src="'+PATHUPLOAD+fileuploaded+'" class="thumbnail" />');		
			//progress.toggleCancel(false);
			var progress = new FileProgress(file, this.customSettings.progressTarget);
			progress.setStatus("Done");	
			$("#"+file.id+">div>.progressName").html('<a class="upload_close" href="#" target="_self" onclick="cancelUpload(\''+file.id+'\',\''+fileuploaded+'\');return false;">&nbsp;&nbsp;</a><img src="'+PATHUPLOAD+fileuploaded+'" class="thumbnail" />');
			uploads[uploads.length]=fileuploaded;
			write2input();
		} catch (ex) {
			alert(ex);
		}
	}
	function uploadStart1(file) {
		try {
			if (!uploadable()){		
				alert("Max file upload is "+MAX_FILE);
				swfu.cancelUpload(file.id,null);
			}
			this.addPostParam("path", FOLDERUPLOAD);
			this.addPostParam("file", FILEUPLOAD+"_"+file.index);		
			var progress = new FileProgress(file, this.customSettings.progressTarget);		
			progress.toggleCancel(true, this);
			progress.setStatus("Uploading...");		
		}
		catch (ex) {}
		
		return true;
	}
	function uploadProgress1(file, bytesLoaded, bytesTotal) {
		try {		
			var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
			var progress = new FileProgress(file, this.customSettings.progressTarget);
			progress.setProgress(percent);
			progress.setStatus("Uploading...");
		} catch (ex) {
			this.debug(ex);
		}
	}

	var settings= {
		flash_url : "<?php echo $this->webroot;?>js/lib/swfupload/swfupload.swf",
		upload_url: "<?php echo $this->webroot;?>files/swfupload.php",
		post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},
		post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},

		// File Upload Settings
		file_size_limit : "500",	// 200 kb
		file_types : "*.jpg;*.gif;*.png",
		file_types_description : "Image Files",				
		file_upload_limit : 10,
		file_queue_limit : MAX_FILE,
		custom_settings : {
			progressTarget : "fsUploadProgress",
			cancelButtonId : "btnCancel"
		},
		debug: false,

		// Button settings
		button_image_url: "<?php echo $this->webroot;?>js/lib/swfupload/bg.png",
		button_text : "<span class=\"upload_text\">Upload photo</span>",
		button_text_style : ".upload_text { color:#FFFFFF;font-size:12px;text-align:center;}", 
		button_text_top_padding : 6, 
		button_class: "upload_button",				
		button_placeholder_id : "spanButtonPlaceHolder",
		button_width: 120,
		button_height: 28,
		
		// The event handler functions are defined in handlers.js
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart1,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess1,
		upload_complete_handler : uploadComplete,
		queue_complete_handler : queueComplete	// Queue plugin event
	};

	function write2input(){
		var i;	
		for(i=0;i<uploads.length;i++){
			$("#PressPhoto"+(eval(i)+1)).val(uploads[i]);
		}
		for(i=uploads.length;i<MAX_FILE;i++){
			$("#PressPhoto"+(eval(i)+1)).val('');
		}	
	}
	function deleteUploadfile(id){	
		var i, found = false;	
		if (uploads[uploads.length-1] != id){
			for(i=0;i<uploads.length;i++){
				if(found){
					uploads[i-1]=uploads[i];
				}else if (uploads[i]==id){
					found=true;
					uploads[i]=uploads[i];
				}	
			}
		}
		uploads.length=uploads.length-1;	
		write2input();
	}
	function uploadable(){
		return uploads.length < MAX_FILE;
	}
	function loadphotos(){
		var i, image;	
		for(i=0;i<MAX_FILE;i++){
			if($("#PressPhoto"+(eval(i)+1)).val()!=""){
				image = $("#PressPhoto"+(eval(i)+1)).val();
				uploads[i]=image;			
				$("#fsUploadProgress").html($("#fsUploadProgress").html()+'<div class="progressWrapper" id="xSWFUpload_0_'+i+'"><div class="progressContainer"><div class="progressName"><a onclick="cancelUpload(\'xSWFUpload_0_'+i+'\',\''+image+'\');return false;" target="_self" href="#" class="upload_close">&nbsp;&nbsp;</a><img src="'+PATHUPLOAD+image+'" class="thumbnail" /></div></div>')
			}
			else break;
		}		
	}

</script>
<script type="text/javascript">
$(document).ready(function(){
try{
	$("#PressAdminEditForm").validate({
		rules: {
			'data[Press][name]': {
				required: true,
				minlength: 3
			},
			'data[Press][offered]': {
				required: true,
				minlength: 3
			},
			'data[Press][serial_number]': {
				required: true
			},
			'data[Press][sup_reference]': {
				required: true
			},
			'data[Press][perfect]': {
				required: true
			},
			'data[Press][year]': {
				required: true,
				number:true
			},
			'data[Press][paper_size2]': {
				cRequired: true
			},
			'data[Press][colour]': {
				required: true
			},
			'data[Press][video]': {
				required: true
			}
			
		},
		messages: {			
		}
	});	
	function pageSizeRequired(){
		pagerSize1=jQuery.trim($("#PressPaperSize1").val());
		pagerSize2=jQuery.trim($("#PressPaperSize2").val());		
		if(pagerSize1==""||pagerSize2=="")
			return false;
		var patt=/^\d+$/;	
		if(patt.test(pagerSize1) && patt.test(pagerSize2))
			return true;
		return false;
	}
	$.validator.addMethod("cRequired",pageSizeRequired,"Page size required");
	$(".datepicker" ).datepicker({
		"dateFormat": "yy-mm-dd",		
		onClose: function(dateText, inst) {
			if ($('#PressFirstOffDateMonth').val()!='')$('#datechose_overlay2').hide();
			if ($('#AvailableFromDate').val()!='')$('#datechose_overlay1').hide();
		}
	});
	$('.jqTransform').jqTransform({width:100});
	swfu = new SWFUpload(settings);  
	loadphotos();
}catch(e){
	alert(e);
}
});
</script>
<?php echo $this->element('menu', array("active" => "3.a"));?>
<ul class="breadcrumb">
        <li>
          <?php echo $this->Html->link(__('Press  ', true), array('action' => 'index'), array('class'=>'fist_link'));?>
        </li>
        <li>
          <a href="#" >Edit Press  </a>
        </li>
</ul>	
<div id="content" class="presses stocklist_form">	
	<?php echo $session->flash();?>
	<?php echo $this->Form->create('Press',array('enctype' => 'multipart/form-data'));?>
	<?php echo $this->Form->hidden('id'); ?>	
	<table>
		<tr>
			<td colspan="2">
				<?php echo $this->Form->input('name'); ?>
				<?php echo $this->Form->input('offered'); ?>
				<?php echo $this->Form->input('serial_number'); ?>
				<span class="jqTransform level17"><?php echo $this->Form->input('press_make_id',array('label'=>'Make','name'=>'data[Press][make]')); ?></span>
				<span class="jqTransform level16"><?php echo $this->Form->input('press_types_id',array('label'=>'Type','name'=>'data[Press][type]')); ?></span>
				<span class="jqTransform level15"><?php echo $this->Form->input('press_model_id',array('label'=>'Model','name'=>'data[Press][model]')); ?></span>				
				<?php echo $this->Form->input('sup_reference'); ?>
				<?php echo $this->Form->input('perfect',array('class'=>'w180')); ?>
				<?php echo $this->Form->input('year'); ?>
				<div class="input text paper_size">
					<label>Paper Size</label>
					<?php echo $this->Form->input('paper_size1',array('div'=>false,'label'=>false,'style'=>'width:50px !important;')); ?>
					&nbsp;x<?php echo $this->Form->input('paper_size2',array('div'=>false,'label'=>false,'style'=>'width:50px !important;')); ?>
				</div>	
				<?php echo $this->Form->input('colour'); ?>
			</td>
		</tr>
	<tr>
			<td colspan="2">
				<div class="input text">
					<label>Photo</label>
				</div>
				<!--upload-->
				<div  id="uploader">
					<div id="thumbnails">
						<div id="thumbnail"></div>
					</div>
					<!--upload-->
					<div id="divStatus" style="display:none;">0 Files Uploaded</div>
					<div class="fieldset flash" id="fsUploadProgress">
						<span class="legend"></span>
					</div>
					<?php
						echo $this->Form->input('photo1',array('type'=>'hidden'));
						echo $this->Form->input('photo2',array('type'=>'hidden'));
						echo $this->Form->input('photo3',array('type'=>'hidden'));
						echo $this->Form->input('photo4',array('type'=>'hidden'));
					?>				
				</div>
			</td>
		</tr>
		<tr><td class="firsttd"></td><td>
			<div id="upload">
				<span id="spanButtonPlaceHolder"></span>
				<input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px; display:none;" />
			</div>
		</td>
		</tr>
		<tr>
			<td colspan="2">
				<?php echo $this->Form->input('video'); ?>
			</td>
		</tr>
		<tr>
			<td colspan="2">			
				<?php echo $this->Form->input('in_stock'); ?>
				<?php echo $this->Form->input('sold'); ?>
				<?php echo $this->Form->input('active'); ?>
			</td>
		</tr>
	<tr>
			<td class="firsttd"></td><td>		
				<div class="clear">
		<?php echo $form->submit(__('Save', true),array('class'=>'bt btinline','div'=>false));?>
		<?php echo $html->link(__('Edit status', true),array('action'=>'editstatus',$this->data["Press"]["id"]),array('class'=>'bt btinline'));?>
	</div>	 
	</td></tr>
	</table>	
	<?php echo $this->Form->end();?>	
</div>